﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class admin_MPowerForm : System.Web.UI.Page
{
    public ArrayList campos_arr = null;
    RE_User ruser = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        Hashtable ht = null;
        int id = 0;
        if (Session["userid"] == null) Response.Redirect("../default.aspx");
        ruser= (RE_User)Session["userid"];
        if (Request.QueryString["id"] != null && !Request.QueryString["id"].ToString().Equals(""))
        {
            id = int.Parse(Request.QueryString["id"].ToString());
            lb_id.Text = id.ToString();
            ht = (Hashtable)DB.getDatabyID(id);
        }


        ArrayList sections = DB.getSections(4);
        TableRow tr = null;
        TableCell tc1 = null, tc2 = null, tc3 = null, tc4 = null, tc5 = null, tc6 = null;
        Label lb_nombre = null;
        TextBox tb = null;
        DropDownList ddl = null;
        Label lb = null;
        RequiredFieldValidator rfv = null;
        RegularExpressionValidator rev = null;
        ListItem item = null;
        ArrayList listado_arr = null;
        int contador = 0;
        TableHeaderCell tdtitulo = null;
        Label lbtitulo = null;
        foreach (RE_GenericBean section in sections)
        {
            tr = new TableRow();
            lbtitulo = new Label();
            lbtitulo.Text = section.strC1;
            tdtitulo = new TableHeaderCell();
            tdtitulo.Style.Add("style", "font-weight: bold; color: white; background-color: #4f1919;height: 13px;");
            //tdtitulo.Style.Add("style", "font-weight: bold; color: white; background-color: #6b696b; background-image:url('images/barra.png');height: 13px;");
            tdtitulo.ColumnSpan = 6;
            tdtitulo.Controls.Add(lbtitulo);
            tr.Cells.Add(tdtitulo);
            Tabla.Rows.Add(tr);
            campos_arr = DB.getCamposFormulario(section.intC1.ToString());
            contador = 0;
            foreach (campos_Bean campo in campos_arr)
            {
                if (contador % section.intC2 == 0) tr = new TableRow();
                contador++;
                tc1 = new TableCell();
                tc2 = new TableCell();
                tc3 = new TableCell();
                tc4 = new TableCell();
                tc5 = new TableCell();
                tc6 = new TableCell();

                tc1.HorizontalAlign = HorizontalAlign.Left;
                tc2.HorizontalAlign = HorizontalAlign.Left;
                tc3.HorizontalAlign = HorizontalAlign.Left;
                tc4.HorizontalAlign = HorizontalAlign.Left;
                tc5.HorizontalAlign = HorizontalAlign.Left;
                tc6.HorizontalAlign = HorizontalAlign.Left;

                lb_nombre = new Label();
                lb_nombre.ID = "lb_" + campo.Nombre;
                lb_nombre.Text = campo.DisplayName;

                tc1.Controls.Add(lb_nombre);
                if (campo.TipoCampo.Equals("DropDown"))
                {
                    ddl = new DropDownList();
                    ddl.ID = campo.Nombre;
                    Label labeldrp = new Label();
                    labeldrp.ID=  "lbldrop_" + campo.Nombre;
                    labeldrp.Text = "Invalid Option";
                    labeldrp.ForeColor = System.Drawing.Color.Red;
                    labeldrp.Visible = false;
                    listado_arr = (ArrayList)DB.getListado(campo.TablaDep, campo.CampoDep, campo.WhereDep);
                    if ((listado_arr != null) && (listado_arr.Count > 0))
                    {
                        item = new ListItem("Select One", "");
                        ddl.Items.Add(item);
                        foreach (RE_GenericBean rgb in listado_arr)
                        {
                            item = new ListItem(rgb.strC1, rgb.strC2);
                            
                            ddl.Items.Add(item);
                        }
                    }

                    tc2.VerticalAlign = VerticalAlign.Middle;
                    tc2.Controls.Add(ddl);
                    tc2.Controls.Add(labeldrp);
                    if ((campo.Nombre.Equals("frmAgentName")))
                    {
                       
                            ddl.SelectedValue = ruser.UserID; 
                        
                       
                    }
                    if (ht != null)
                    {
                        if (ht.ContainsKey(ddl.ID.ToString()))
                            ddl.SelectedValue = ht[ddl.ID.ToString()].ToString();
                    }
                    if ((campo.Nombre.Equals("frmUtility")))
                    {
                        ddl.SelectedIndex=1;

                    }
                }
                else
                {
                    if (campo.TipoCampo.Equals("Label"))
                    {
                        lb = new Label();
                        lb.ID = campo.Nombre;
                       
                        
                        lb.Width = campo.Size;
                        tc2.VerticalAlign = VerticalAlign.Middle;
                        tc2.Controls.Add(lb);
                        /* if (ht != null)
                         {
                             if (ht.ContainsKey(ddl.ID.ToString()))
                          //       lb.Text = ht[lb.ID.ToString()].ToString();
                         }*/
                    }
                    else
                    {
                        tb = new TextBox();
                        tb.ID = campo.Nombre;
                        if ((campo.Nombre.Equals("frmDateIn")) || (campo.Nombre.Equals("frmQCDate")))
                        {
                            tb.Text = DateTime.Now.ToString("MM/dd/yy");
                            tb.Enabled = false;
                           
                            
                        }
                        if ((campo.Nombre.Equals("frmAppTime")) )
                        {
                            tb.Text = DateTime.Now.ToString("HH:mm");
                            tb.Enabled = false;


                        }
                        if ((campo.Nombre.Equals("frmComments3")))
                        {
                            tb.TextMode = TextBoxMode.MultiLine;
                            tb.Rows = 7;


                        }
                        if ((campo.Nombre.Equals("frmAgentId")))
                        {

                            tb.Enabled = false;

                        }

                       
                        tb.Width = campo.Size;
                        tb.MaxLength = campo.MaxLen;
                        tc2.VerticalAlign = VerticalAlign.Middle;
                        tc2.Controls.Add(tb);
                        tc2.ColumnSpan = campo.Rows;
                        if (ht != null)
                        {
                            if (ht.ContainsKey(tb.ID.ToString()))
                                tb.Text = ht[tb.ID.ToString()].ToString();
                        }
                        if (campo.AllowNull == 0)
                        {
                            rfv = new RequiredFieldValidator();
                            rfv.ID = "rfv_" + campo.Nombre;
                            rfv.ErrorMessage = "This field is required";
                            rfv.ControlToValidate = campo.Nombre;
                            rfv.SetFocusOnError = true;
                            tc2.Controls.Add(rfv);
                        }
                        if (campo.ValidaExpr == 1)
                        {
                            rev = new RegularExpressionValidator();
                            rev.ID = "rev_" + campo.Nombre;
                            if (campo.MessageError.Equals(""))
                                rev.ErrorMessage = "Incorrect Format";
                            else
                            {
                                rev.ErrorMessage = campo.MessageError;
                            }
                            rev.ControlToValidate = campo.Nombre;
                            rev.ValidationExpression = campo.Expresion_Reg;
                            tc2.Controls.Add(rev);
                        }
                    }
                }
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                Tabla.Rows.Add(tr);
                Tabla.HorizontalAlign = HorizontalAlign.Center;
            }
        }
    }
    protected void btn_save_search_Click(object sender, EventArgs e)
    {
        limpiar();
        if (validarCombos()==1) { 
            int respuesta = Guardar_datos();
            if (respuesta == -1)
            {
                msg.Text = "Error, try again please";
                return;
            }
        Response.Redirect("buscadorMpower.aspx");
        }
    }
    private int Guardar_datos()
    {
        int result = 0;
        TextBox tb = null;
        DropDownList ddl = null;
        string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        xml += "<campos>";
        xml += "<client>MPowerEnergy</client>";

        foreach (Control tabla in Tabla.Controls)
        {
            foreach (Control rows in tabla.Controls)
            {
                foreach (Control child in rows.Controls)
                {

                    if (child is TextBox)
                    {
                        tb = (TextBox)child;
                        if (tb.Text != null && !tb.Text.Trim().Equals(""))
                        { xml += "<" + tb.ID + ">" + tb.Text.ToUpper() + "</" + tb.ID + ">"; }
                        else{
                         
                            if (!tb.ID.Equals("frmAgentId"))
                            { 
                                xml += "<" + tb.ID + " />";
                            }
                        }
                    }


                    else if (child is DropDownList)
                    {
                        ddl = (DropDownList)child;
                        xml += "<" + ddl.ID + ">" + ddl.SelectedValue + "</"+ddl.ID+">";
                        if (ddl.ID.Equals("frmAgentName")){
                             xml += "<frmAgentId>" + ddl.SelectedValue  + "</frmAgentId>"; 
                        }
                    }

                }
            }
        }
        xml += "</campos>";
        int id = int.Parse(lb_id.Text);

        return DB.InsetUpdateData(id, xml);
    }
    protected void bt_delete_Click(object sender, EventArgs e)
    {
        int id = int.Parse(lb_id.Text);
        if (id == 0)
        {
            msg.Text = "Error.";
            msg.Visible = true;
            return;
        }

        int result = DB.deleteData(id);
        if (result != 1)
        {
            msg.Text = "Error.  Can't delete this item, please try again";
            msg.Visible = true;
            return;
        }
        Response.Redirect("buscadorMPower.aspx");
    }
    private int validarCombos() {
        int result = 1;
        DropDownList ddl = (DropDownList)Tabla.FindControl("frmAgentName");
       
        
        if (ddl.SelectedItem.ToString().ToUpper().Equals("SELECT ONE")) {
            Label lab = (Label)Tabla.FindControl("lbldrop_frmAgentName");
            lab.Visible = true;
            return 0; 
        }
        DropDownList ddl2 = (DropDownList)Tabla.FindControl("frmTypeSale2");
        if (ddl2.SelectedItem.ToString().ToUpper().Equals("SELECT ONE")) {
            Label lab = (Label)Tabla.FindControl("lbldrop_frmTypeSale2");
            lab.Visible = true;
            return 0; 
        }
        DropDownList ddl3 = (DropDownList)Tabla.FindControl("frmUtility");
        if (ddl3.SelectedItem.ToString().ToUpper().Equals("SELECT ONE")) {
            Label lab = (Label)Tabla.FindControl("lbldrop_frmUtility");
            lab.Visible = true;    
            return 0;
        
        }
     /*   DropDownList ddl4 = (DropDownList)Tabla.FindControl("frmQCApproval");
        if (ddl4.SelectedItem.ToString().ToUpper().Equals("SELECT ONE")) {
            Label lab = (Label)Tabla.FindControl("lbldrop_frmAgentName");
            lab.Visible = true;
            return 0; 
        }
        DropDownList ddl5 = (DropDownList)Tabla.FindControl("frmQCStatus");
        if (ddl4.SelectedItem.ToString().ToUpper().Equals("SELECT ONE")) {
            Label lab = (Label)Tabla.FindControl("lbldrop_frmAgentName");
            lab.Visible = true;    
            return 0;
        }

        */

        return result;
    }
    private void limpiar() {
        Label lab = (Label)Tabla.FindControl("lbldrop_frmAgentName");
        lab.Visible = false;
        Label lab2 = (Label)Tabla.FindControl("lbldrop_frmTypeSale2");
        lab2.Visible = false;
        Label lab3 = (Label)Tabla.FindControl("lbldrop_frmUtility");
        lab3.Visible = false;

    
    }
}